#!/bin/sh

main()
{
. $DOCUMENT_ROOT/apps/home/header.tpl.sh 2>/dev/null
header_html 2>/dev/null
cat <<'EOF'
<script>
$(function(){
  $('#restore_factiory_default_button').on('click', function(e){
  var url = 'index.cgi?app=firmware&action=post_restore_factiory_default_confirm';
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    $('#post_restore_factiory_default_confirm').html(data);
  }, 1);
});
});
</script>
EOF
cat <<EOF
		<div class="pure-g">
			<div class="l-box-lrg pure-u-1 pure-u-md-1-2">
			
				<h3>升级固件</h3>
				<form action="/apps/firmware/upload.cgi" method="post" enctype="multipart/form-data">
				<label for="file">上传固件:</label>
				<input type="file" name="file"><p>
				<input type="submit" name="submit" class="pure-button" value="上传">
				</form>

			</div>
			<div class="l-box-lrg pure-u-1 pure-u-md-1-2">
				<div class="content">
				<h3>恢复出厂设置<h3>
					<button class="pure-button" id="restore_factiory_default_button">恢复出厂设置</button>
					<div id="post_restore_factiory_default_confirm"></div>
				</div>
			</div>
		</div>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
EOF

footer_html

}
post_restore_factiory_default_confirm()
{
cat <<'EOF'
<script>
$(function(){
  $('#post_do_firstboot').on('submit', function(e){
    e.preventDefault();
    var data = "app=firmware&"+$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'post_do_firstboot');
  });
});
</script>
EOF
post_savefile_html
cat <<EOF

<p>你确认要恢复出厂设置吗?</p>
<form id="post_do_firstboot">
<input type="hidden" name="app" value="firmware"></input>
<input type="hidden" name="action" value="post_do_firstboot"></input>
<button type="submit" class="pure-button button-error">确认</button>
</form>
EOF
}
post_do_firstboot()
{
main.sbin first_boot >/dev/null 2>&1
(sleep 1 && reboot) >/dev/null 2>&1 &
echo "_LANG_Save_success" | main.sbin output_json 0
}

post_flash()
{
. $DOCUMENT_ROOT/apps/home/header.tpl.sh
header_html
cat <<EOF
<script>setTimeout("location.href='/'",120000)</script>
		<div class="pure-g">
			<div class="content">
				<br/>
				<br/>
				<br/>
				<br/>
				<br/>
				<h1>请耐心等待5分钟,此过程不能断开电源!!</h1>

			</div>
		</div>
EOF
footer_html keep_bottom
main.sbin flash_firmware
}

post_keep_files()
{
echo '' > /etc/sysupgrade.conf
env | grep "^FORM_file_" | cut -d '=' -f2 | grep -v "/etc/sysupgrade.conf" > /etc/sysupgrade.conf
echo "$FORM_filelist" | while read file
do
[ -f ${file} ] && echo ${file} >> /etc/sysupgrade.conf
done
echo "Success" | main.sbin output_json 0
}
lang=`main.sbin get_client_lang`
eval `cat $DOCUMENT_ROOT/apps/$FORM_app/i18n/$lang/i18n.conf 2>/dev/null` 
. $DOCUMENT_ROOT/apps/firmware/firmware_lib.sh
if
[ $is_main_page = 1 ]
then
main
elif [ -n "$FORM_action" ]
then
$FORM_action
fi
